package org.example.task.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.task.converter.TaskCommentConverterMapper;
import org.example.task.mapper.TaskCommentMapper;
import org.example.task.model.dto.taskcomment.CreateTaskComment;
import org.example.task.model.po.LoginUser;
import org.example.task.model.po.TaskComment;
import org.example.task.service.ITaskCommentService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
@RequiredArgsConstructor
public class TaskCommentServiceImpl extends ServiceImpl<TaskCommentMapper, TaskComment> implements ITaskCommentService {
    private final TaskCommentConverterMapper converterMapper;

    @Override
    public boolean createComment(CreateTaskComment comment) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        TaskComment taskComment = converterMapper.createTaskCommentToTaskComment(comment);
        taskComment.setCommentTime(LocalDateTime.now());
        taskComment.setUserId(loginUser.getUser().getUserId());
        return save(taskComment);
    }
}
